package com.tianxin.workbao.workBao.Activity;

import android.os.Bundle;
import androidx.annotation.Nullable;
import com.google.android.material.floatingactionbutton.FloatingActionButton;
import com.google.android.material.tabs.TabLayout;
import androidx.viewpager.widget.ViewPager;
import androidx.appcompat.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;

import com.tianxin.workbao.workBao.R;
import com.tianxin.workbao.workBao.adapter.FragmentStareAdapter;
import com.tianxin.workbao.workBao.fragment.BackBillFragment;

/**
 * Created by hacker on 2017/6/22.
 */

public class BackBillListActivity extends BaseActivity {

    private TabLayout mTabLayout;
    private ViewPager mViewPager;
    private FloatingActionButton mFab;
    private FragmentStareAdapter mAdapter;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        super.initStatusBar(true);
        setContentView(R.layout.common_main_tablayout);

        initData();
        initView();
        initEvent();
        bindView();
    }

    public void initView() {
        mToolbar = findViewById(R.id.toolbar);
        mToolbar.setTitle("");
        mToolbar.setNavigationIcon(R.drawable.ic_action_back_24dp);
        setSupportActionBar(mToolbar);

        mViewPager = findViewById(R.id.viewPager);
        mViewPager.setOffscreenPageLimit(2);
        mViewPager.setCurrentItem(0);

        mTabLayout = findViewById(R.id.tabLayout);
        mTabLayout.setTabGravity(TabLayout.GRAVITY_CENTER);
        mTabLayout.setupWithViewPager(mViewPager);
        mTabLayout.setSelectedTabIndicatorColor(getResources().getColor(R.color.write));

        mFab = findViewById(R.id.top_fab);
    }

    @Override
    public void initData() {
        mFragments.add(0, BackBillFragment.newInstance(0));
        mFragments.add(1, BackBillFragment.newInstance(1));
        mTabTitles.add(0, "回单录入");
        mTabTitles.add(1, "回单管理");
    }

    @Override
    public void initEvent() {
        //navigationIcon event
        mToolbar.setNavigationOnClickListener(view -> BackBillListActivity.this.finish());
        // fab 刷新
        mFab.setOnClickListener(view -> deepRefresh());
        mTabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                searchHint ="客户的部分名称汉字或车牌号数字部分";
                searchTag = tab.getPosition();
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
       getMenuInflater().inflate(R.menu.menu_search,menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.search:
                showSearchView(this,getSearchHandlerListener());
                break;
        }
        return super.onOptionsItemSelected(item);
    }

    /**
     * 深度刷新
     */
    private void deepRefresh() {
        updateUi();
    }

    private void updateUi() {
        mFragments.get(mTabLayout.getSelectedTabPosition()).initData(false);
    }

    @Override
    public void bindView() {
        mAdapter = new FragmentStareAdapter(getSupportFragmentManager(), mFragments, mTabTitles);
        mViewPager.setAdapter(mAdapter);
    }

    @Override
    protected void onRestart() {
        updateUi();
        super.onRestart();
    }
}
